/*
	Figure 1. Effect of Candidate Attributes on Perceived Candidate Liberalness
	Corresponds to Table S1a and Table S1b in Appendix
	
*/


use data_study_1.dta, clear
	
// Set omitted categories for coefplot
	gen zero_race = 0
	label var zero_race "White"
	gen zero_sex = 0
	label var zero_sex "Male"
	gen zero_eco = 0
	label var zero_eco "Maintain investment in energy"
	gen zero_aa = 0
	label var zero_aa "Not shown policy"

	// Set regression variables
	loc reg_race "cand_black zero_race"
	loc reg_sex "cand_female zero_sex"
	loc reg_issues "cand_policy_abort1 cand_policy_abort2 cand_policy_tax1 cand_policy_tax2"
	loc reg_issues "`reg_issues' cand_policy_health1 cand_policy_health2 cand_policy_eco1 zero_eco"
	loc reg_affirm "cand_policy_aa1 cand_policy_aa2 cand_policy_aa3 zero_aa"
	
// Interactive Terms for Black Candidate X Affirmative Action
	gen noracepolicy = cand_policy_aa1 == 0 & cand_policy_aa2 == 0 & cand_policy_aa3 == 0	
	gen black_aa1 = cand_policy_aa1*cand_black
	gen black_aa2 = cand_policy_aa2*cand_black
	gen black_aa3 = cand_policy_aa3*cand_black
	gen white_aa1 = cand_policy_aa1*cand_white
	gen white_aa2 = cand_policy_aa2*cand_white
	gen white_aa3 = cand_policy_aa3*cand_white
	label var black_aa1 "Black X Expand (race)"
	label var black_aa2 "Black X Keep as is"
	label var black_aa3 "Black X Replace (class)"
	label var white_aa1 "White X Expand (race)"
	label var white_aa2 "White X Keep as is"
	label var white_aa3 "White X Replace (class)"
	label var noracepolicy "Not shown position"
	
	loc int "white_aa1 white_aa2 white_aa3 black_aa1 black_aa2 black_aa3"
	
	gen out_fair_bwdiff = out_fair_black - out_fair_white
	
//============================================================================== No interaction

	 reg out_ideo_7 cand_black zero_race cand_female zero_sex cand_policy_abort1 ///
		cand_policy_abort2 cand_policy_tax1 cand_policy_tax2 ///
		cand_policy_health1 cand_policy_health2 cand_policy_eco1 zero_eco ///
		cand_policy_aa1 cand_policy_aa2 cand_policy_aa3 zero_aa cand_age, robust

	
	// Ideology
	reg out_ideo_7 `reg_race' `reg_sex' `reg_issues' `reg_affirm' cand_age, robust
	eststo ideo
		loc b_ideo = string(round(_b[_cons], 0.001), "%9.3f") // store mean values
	outreg2 using table_s1_fig1results.xls, replace ctitle(Ideology)
	
	// Issue Priority (social justice)
	reg out_priority_sj `reg_race' `reg_sex' `reg_issues' `reg_affirm' cand_age, robust
	eststo sj
		loc b_sj = string(round(_b[_cons], 0.001), "%9.3f")	
	outreg2 using table_s1_fig1results.xls, append ctitle(Social Justice Priority)
	
	reg out_fair_bwdiff `reg_race' `reg_sex' `reg_issues' `reg_affirm' cand_age, robust
	eststo fairness
		loc b_fair = string(round(_b[_cons], 0.001), "%9.3f")	
	outreg2 using table_s1_fig1results.xls, append ctitle(Fairness to Black over Whites)
	

	coefplot (ideo, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
					, bylabel("{bf:(a) Ideological Liberalness}") || ///
			 (sj, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
				, bylabel("{bf:(b) Prioritize Social Justice}") || ///
			 (fairness, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
				, bylabel("{bf:(c) Fairer to Black than White Constituents}") ///
			|| , drop(_cons cand_age) omitted baselevels ms(c) msize(medsmall) ylabel(,labsize(vsmall)) ///
		xline(0, lc(black)) nokey ///
		byopts(row(1) t1title("{bf:`title'}", size(small))) ///
		subtitle(, bcolor(white) color(black) size(vsmall)) ///
		xtitle("Effects of Candidate Attributes (Scale 0 to 1)", size(vsmall)) ///
		legend(order(2 "Democrats" 4 "Republicans") size(vsmall) position(bottom))  ///
		xlabel(-0.1(0.05)0.15,labsize(small)) norecycle ///
		headings(cand_black = "{bf: Race}" cand_female = "{bf: Sex}" ///
			cand_policy_abort1 = "{bf: Abortion}" cand_policy_tax1 = "{bf: Tax Policy}" ///
			cand_policy_health1 = "{bf: Health Care}" cand_policy_eco1 = "{bf: Renewable Energy}" ///
			cand_policy_aa1 = "{bf: Affirmative Actions}", labsize(vsmall)) ///
			saving(lucid.gph, replace) 
		
	addplot 1: ,note("Mean, Baseline Categories = `b_ideo'", size(vsmall)) norescaling
	addplot 2: ,note("Mean, Baseline Categories = `b_sj'", size(vsmall)) norescaling		
	addplot 3: ,note("Mean, Baseline Categories = `b_fair'", size(vsmall)) norescaling		
	
	graph display, xsize(4.5) ysize(3.4) margins(vsmall)	
	graph export figure_1.png, as(png) replace

/*
	Figure 2. Effect of Interaction (AA position x candidate race)
		- Corresponds to Table S2
		
	Also creates Table S3 (additional issue priority outcomes)
*/

use data_study_1.dta, clear

// Set omitted categories
	gen zero_race = 0
	label var zero_race "White"
	gen zero_sex = 0
	label var zero_sex "Male"
	gen zero_eco = 0
	label var zero_eco "Maintain investment in energy"
	
// Set regression variables
	loc reg_race "cand_black zero_race"
	loc reg_sex "cand_female zero_sex"
	loc reg_issues "cand_policy_abort1 cand_policy_abort2 cand_policy_tax1 cand_policy_tax2"
	loc reg_issues "`reg_issues' cand_policy_health1 cand_policy_health2 cand_policy_eco1 zero_eco"
	loc reg_affirm "cand_policy_aa1 cand_policy_aa2 cand_policy_aa3"
	
// Interactive Terms for Black Candidate X Affirmative Action
	gen noracepolicy = cand_policy_aa1 == 0 & cand_policy_aa2 == 0 & cand_policy_aa3 == 0	
	gen black_aa1 = cand_policy_aa1*cand_black
	gen black_aa2 = cand_policy_aa2*cand_black
	gen black_aa3 = cand_policy_aa3*cand_black
	gen white_aa1 = cand_policy_aa1*cand_white
	gen white_aa2 = cand_policy_aa2*cand_white
	gen white_aa3 = cand_policy_aa3*cand_white
	gen black_aa0 = noracepolicy*cand_black
	gen white_aa0 = noracepolicy*cand_white
	label var black_aa0 "Black X No Position"
	label var white_aa0 "White X No Position"
	label var black_aa1 "Black X Expand (race)"
	label var black_aa2 "Black X Keep as is"
	label var black_aa3 "Black X Replace (class)"
	label var white_aa1 "White X Expand (race)"
	label var white_aa2 "White X Keep as is"
	label var white_aa3 "White X Replace (class)"
	replace white_aa0 = 0
	label var noracepolicy "Not shown position"
	loc int "white_aa0 white_aa1 white_aa2 white_aa3 black_aa0 black_aa1 black_aa2 black_aa3"
	
	gen out_fair_bwdiff = out_fair_black - out_fair_white
		
		
// Run regressions to get estimates using eststo, while also creating appendix tables
	// Column 1 (Ideological Liberalness)
	reg out_ideo_7 `reg_sex' `reg_issues' `int' cand_age, robust
	eststo ideo
		loc b_ideo = string(round(_b[_cons], 0.001), "%9.3f")
	outreg2 using table_s2_fig2results.xls, replace ctitle(Ideological Liberalness)	
	
	// Column 2 (SJ priority) + additional outcomes for Table S3
	loc c = 0
	d out_priority*, varlist
	foreach var in `r(varlist)' {
		reg `var' `reg_sex' `reg_issues' `int' cand_age , robust
		eststo r_`var'
		if `c' == 0 outreg2 using table_s3.xls, replace ctitle(`var')
		if `c' != 0 outreg2 using table_s3.xls, append ctitle(`var')
		if "`var'" == "out_priority_sj" {
			loc b_sj = string(round(_b[_cons], 0.001), "%9.3f")
			outreg2 using table_s2_fig2results.xls, append ctitle(Social Justice)
		}
		loc c = 1
	}
		
	// Column 3 (fairness)
	reg out_fair_bwdiff `reg_sex' `reg_issues' `int' cand_age , robust
		outreg2 using table_s2_fig2results.xls, append ctitle(Fairness)
		eststo r_out_fair_bwdiff
		loc b_fair = string(round(_b[_cons], 0.001), "%9.3f")

	// Fairness and Issue priority
	foreach var of varlist out_fair_bwdiff out_priority_* {
		loc s = subinstr("`var'", "out_fair_", "", .)
		loc s = subinstr("`s'", "out_priority_", "", .)
		
		// Fairness
		if "`s'" == "bwdiff" loc tit "(b) Perceived Group Fairness: (Fairness to white) - (Fairness to Black)"
		// Priority
		if "`s'" == "tax" loc tit "Tax Policy"
		if "`s'" == "job" loc tit "Job Creation"
		if "`s'" == "health" loc tit "Healthcare"
		if "`s'" == "enviro" loc tit "Environmental Policy"
		if "`s'" == "abort" loc tit "Abortion"
		if "`s'" == "crim" loc tit "Criminal Justice Reform"
		if "`s'" == "sj" loc tit "Social Justice Issues"
			
		coefplot (r_`var', mc(gs4) ciopts(lw(med) color(gs4))) ///
			, drop(_cons `reg_issues' `reg_sex' cand_age) omitted baselevels ms(c) msize(s) ylabel(,labsize(vsmall)) ///
			xline(0, lc(black)) nokey ///
			subtitle("{bf:`tit'}", bcolor(white) size(small) box pos(12) ///
				bmargin(small) bexpand margin(small)) ///
			headings(white_aa0 = "{bf: No Affirmative Action}" ///
					white_aa1 = "{bf: White X Affirmative Action}" ///
					black_aa1 = "{bf: Black X Affirmative Action}", labsize(vsmall)) ///
			xlabel(-0.2(0.1)0.2,labsize(small)) norecycle saving(f_`var'.gph, replace)
	}
	
	coefplot (ideo, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
				, bylabel("{bf:(a) Ideological Liberalness}") || ///
			 (r_out_priority_sj, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
				, bylabel("{bf:(b) Prioritize Social Justice}") || ///
			(r_out_fair_bwdiff, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
				, bylabel("{bf:(c) Fairer to Black than White Constituents}") ///
			|| , drop(_cons `reg_sex' `reg_issues' cand_age) omitted baselevels ms(c) msize(med) ///
			ylabel(,labsize(vsmall)) ///
		xline(0, lc(black)) nokey ///
		subtitle(, bcolor(white) color(black) size(vsmall)) ///
		byopts(row(1) t1title("{bf:`title'}", size(small))) ///
		xtitle("Effects of Candidate Attributes (Scale 0 to 1)", size(vsmall)) ///
		xlabel(-0.2(0.1)0.2,labsize(small)) norecycle ///
		headings(white_aa0 = "{bf: White X Affirmative Action}" ///
			black_aa0 = "{bf: Black X Affirmative Action}", labsize(vsmall))

		addplot 1: ,note("Mean, Baseline Categories = `b_ideo'", size(vsmall)) norescaling
		addplot 2: ,note("Mean, Baseline Categories = `b_sj'", size(vsmall)) norescaling		
		addplot 3: ,note("Mean, Baseline Categories = `b_fair'", size(vsmall)) norescaling		

		graph display, xsize(4.5) ysize(3.4) margins(vsmall)	
		graph export figure_2.png, as(png) replace
	
	
/* 
	Figure S1b. Pre-registered specification of Figure 1.
*/

	use data_study_1.dta, clear

// Set omitted categories
	gen zero_race = 0
	label var zero_race "White"
	gen zero_sex = 0
	label var zero_sex "Male"
	gen zero_eco = 0
	label var zero_eco "Maintain investment in energy"
	gen zero_aa = 0
	label var zero_aa "Not shown policy"

// Create pairings (1 = liberal, 2 = moderate)
	cap drop policy*
	egen policy1 = rowtotal(cand_policy_abort1 cand_policy_tax1 cand_policy_health1 cand_policy_eco1)
	egen policy2 = rowtotal(cand_policy_abort2 cand_policy_tax2 cand_policy_health2 cand_policy_eco2)

	gen policy_LL = policy1 == 2
	gen policy_LM = policy1 == 1 & policy2 == 1
	gen policy_MM = policy2 == 2

	
	// Set regression variables
	loc reg_race "cand_black zero_race"
	loc reg_sex "cand_female zero_sex"
	loc reg_issues "policy_LM policy_MM"
	loc reg_affirm "cand_policy_aa1 cand_policy_aa2 cand_policy_aa3 zero_aa"
	
// Interactive Terms for Black Candidate X Affirmative Action
	gen noracepolicy = cand_policy_aa1 == 0 & cand_policy_aa2 == 0 & cand_policy_aa3 == 0	
	gen black_aa1 = cand_policy_aa1*cand_black
	gen black_aa2 = cand_policy_aa2*cand_black
	gen black_aa3 = cand_policy_aa3*cand_black
	gen white_aa1 = cand_policy_aa1*cand_white
	gen white_aa2 = cand_policy_aa2*cand_white
	gen white_aa3 = cand_policy_aa3*cand_white
	label var black_aa1 "Black X Expand (race)"
	label var black_aa2 "Black X Keep as is"
	label var black_aa3 "Black X Replace (class)"
	label var white_aa1 "White X Expand (race)"
	label var white_aa2 "White X Keep as is"
	label var white_aa3 "White X Replace (class)"
	label var noracepolicy "Not shown position"
	
	loc int "white_aa1 white_aa2 white_aa3 black_aa1 black_aa2 black_aa3"
	
	gen out_fair_bwdiff = out_fair_black - out_fair_white

	// Table
	reg out_ideo_7 `reg_race' `reg_sex' cand_age `reg_issues' `reg_affirm', robust
	outreg2 using table_s1b.xls, replace ctitle(Ideological Liberalness)
	
	reg out_priority_sj `reg_race' `reg_sex' cand_age `reg_issues' `reg_affirm', robust
	outreg2 using table_s1b.xls, append ctitle(Prioritize Social Justice)
	
	reg out_fair_bwdiff `reg_race' `reg_sex' cand_age `reg_issues' `reg_affirm', robust
	outreg2 using table_s1b.xls, append ctitle(Fairness to Black over White)
	